﻿using SignalBox.Core.Model;
using SignalBox.Core.Model.Timetable;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SignalBoxSimulator
{
    public partial class frmMain : Form
    {
        private frmLayout _FLayout;
        private TrainList _trainList;
        private TrainDetails _trainDetails;
        private Alerts _alerts;

        private SimulationController simulationController;
        private TrackLayout _trackLayout;

        public frmMain()
        {
            InitializeComponent();
            _FLayout = new frmLayout();

            _trainDetails = new TrainDetails();
            _trainDetails.Show(dockPanel1);
            _trainDetails.DockState = WeifenLuo.WinFormsUI.Docking.DockState.DockBottom;
            _trainDetails.FormClosing += _trainList_FormClosing;

            _trainList = new TrainList(_trainDetails);
            _trainList.FormClosing += _trainList_FormClosing;
            _trainList.Show(dockPanel1);
            _trainList.DockState = WeifenLuo.WinFormsUI.Docking.DockState.DockBottom;




            _alerts = new Alerts();
            _alerts.Show(dockPanel1);
            _alerts.DockState = WeifenLuo.WinFormsUI.Docking.DockState.DockBottom;
            _alerts.FormClosing += _trainList_FormClosing;

            _trackLayout = new TrackLayout(false);
            _trackLayout.ShowGrid = true;
            _FLayout.LayoutPanel.TrackLayout = _trackLayout;

            //_FLayout.MdiParent = this;
            //_FLayout.WindowState = FormWindowState.Maximized;
            _FLayout.Show(dockPanel1);
            _FLayout.DockState = WeifenLuo.WinFormsUI.Docking.DockState.Document;

            chkShowGrid.Checked = true;

            simulationController = new SimulationController(_trackLayout, _trainList, _trainDetails, _alerts, _FLayout.LayoutPanel);

        }

        void LayoutPanel_MouseUp(object sender, MouseEventArgs e)
        {
            
        }

        void _trainList_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
        }

        private void frmMain_Load(object sender, EventArgs e)
        {

        }

        private void btnOpenTrack_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Signalbox Track Files|*.stf|All Files|*.*";
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                FileInfo fi = new FileInfo(dlg.FileName);
                _trackLayout = new TrackLayout(false);
                _trackLayout.loadLayout(fi);
                _trackLayout.ShowGrid = true;
                _FLayout.LayoutPanel.TrackLayout = _trackLayout;
                simulationController = new SimulationController(_trackLayout, _trainList, _trainDetails, _alerts, _FLayout.LayoutPanel);
                simulationController.UpdateCoords += simulationController_UpdateCoords;
                simulationController.UpdateData += simulationController_UpdateData;
                _FLayout.LayoutPanel.Invalidate();
                speedUpDown.Value = "1";

            }
        }

        void simulationController_UpdateData()
        {

            lblTime.Text = simulationController.CurrentTime.ToString("HH:mm:ss");

            List<TrainInfo> infos = new List<TrainInfo>();

            foreach (Train t in simulationController.Trains)
            {

                TrainInfo ti = new TrainInfo();
                ti.Train = t;
                infos.Add(ti);
            }

            _trainList.Trains = infos;

        }

        void simulationController_UpdateCoords(string message)
        {
            lblCoordinates.Text = message;
        }

        private void btnOpenTimetable_Click(object sender, EventArgs e)
        {

            if (simulationController == null) return;

            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Signalbox Timetable (*.stt)|*.stt|All Files (*.*)|*.*";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                Timetable tt = new Timetable(_trackLayout);
                tt.FileName = dlg.FileName;
                tt.Load();
                simulationController.SetTimeTable(tt);
            }
        }

        private void btnStartSimulation_Click(object sender, EventArgs e)
        {
            if (simulationController == null) return;

            if (!simulationController.IsRunning)
            {
                simulationController.StartSimulation();
                btnStartSimulation.Text = "Stop";
            }
            else
            {
                simulationController.StopSimulation();
                btnStartSimulation.Text = "Start";
            }
        }

        private void speedUpDown_UpButtonClicked(object sender, MouseEventArgs e)
        {
            int value = Convert.ToInt32(speedUpDown.Value);

            if (value < 100)
                value++;

            speedUpDown.Value = value.ToString();
            speedUpDown.TextBoxText = value.ToString();
            simulationController.Speed = value;
        }

        private void speedUpDown_DownButtonClicked(object sender, MouseEventArgs e)
        {
            int value = Convert.ToInt32(speedUpDown.Value);

            if (value > 1)
                value--;

            speedUpDown.Value = value.ToString();
            speedUpDown.TextBoxText = value.ToString();
            simulationController.Speed = value;
        }

        private void chkShowGrid_CheckBoxCheckChanged(object sender, EventArgs e)
        {
            _trackLayout.ShowGrid = chkShowGrid.Checked;
            _FLayout.Repaint();
        }
    }
}
